﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Data;

namespace Top.PointStore.Business
{
    /*功能说明:产品图片操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>产品图片
    /// </summary>
    public static class ProductPicManager
    {
        /// <summary>添加
        /// <para>新增成功返回大于0</para>
        /// <para>新增失败返回小于等于0</para>
        /// <para>-1:商品id有误</para>
        /// <para>-2:产品图片kid有误</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<int> Add(T_ProductPic model)
        {
            var checkRes = CheckingT_ProductPic(model);
            if (checkRes < 0) return checkRes;

            using (var db = new DbTour())
            {
                const string sql = "insert into T_ProductPic (F_ProductKid,F_PicUrl) values ({0:F_ProductKid},{0:F_PicUrl})";
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }

        /// <summary>修改
        /// <para>修改成功返回大于0</para>
        /// <para>修改失败返回小于等于0</para>
        /// <para>-1:商品id有误</para>
        /// <para>-2:产品图片kid有误</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<int> Update(T_ProductPic model)
        {
            var checkRes = CheckingT_ProductPic(model);
            if (checkRes < 0) return checkRes;
            using (var db = new DbTour())
            {
                const string sql = "update T_ProductPic set  F_ProductKid={0:F_ProductKid},F_PicUrl ={0:F_PicUrl} where Kid={0:Kid}";
                return await db.Sql(sql, model).ExecuteScalar<int>();
            }
        }

        /// <summary>T_ProductPic模型参数验证
        /// <para>-1:商品id有误</para>
        /// <para>-2:产品图片kid有误</para>
        /// <para>验证成功返回0</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static int CheckingT_ProductPic(T_ProductPic model)
        {
            if (model.F_ProductKid <= 0) return -1;
            if (model.Kid < 0) return -2;
            return 0;
        }
        /// <summary>得到一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<T_PointMain_new> Get(int kid)
        {
            if (kid < 0) return null;
            const string sql = @"select  * from T_ProductPic where Kid = {0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).FirstOrDefault<T_PointMain_new>();
            }
        }

        /// <summary>删除一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<int> Delete(int kid)
        {
            if (kid < 0) return 0;
            const string sql = @"delete from T_ProductPic where Kid = {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).ExecuteNonQuery();
            }
        }

        /// <summary>批量删除
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        public static async Task<int> Delete(string kids)
        {
            if (kids == "") return 1;
            const string sql = @"delete from T_ProductPic where Kid in {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kids).ExecuteNonQuery();
            }
        }

        /// <summary>根据产品编号获取数据列表
        /// </summary>
        /// <param name="productKid"></param>
        /// <returns></returns>
        public static async Task<List<T_ProductPic>> GetList(int productKid)
        {
            if (productKid <= 0) return null;
            const string sql = @"select * from T_ProductPic where F_ProductKid = {0}";
            using (var db = new DbTour())
            {
                var list = await db.Sql(sql, productKid).ToList<T_ProductPic>();
                //foreach (var productPic in list)
                //{
                //    productPic.Thumbs = IOHelper.GetThumbList(productPic.F_PicUrl);
                //}
                return list;
            }
        }
    }
}
